package com.koron.common.core.util.mail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

/**
 * @ClassName MailUtil
 * @description
 * @Author zhouj
 * @Date 2021/4/27 10:21
 */
@Component
public class MailUtil {

    @Autowired
    private MailConfigProperties mailConfigProperties;

    /**
     * 邮件发送器
     *
     * @return 配置好的工具
     */
    private JavaMailSenderImpl createMailSender() {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost(mailConfigProperties.getMailHost());
        sender.setPort(mailConfigProperties.getMailPort());
        sender.setUsername(mailConfigProperties.getMailUsername());
        sender.setPassword(mailConfigProperties.getMailPassword());
        sender.setDefaultEncoding("Utf-8");
        Properties p = new Properties();
        p.setProperty("mail.smtp.timeout", mailConfigProperties.getTimeout());
        p.setProperty("mail.smtp.auth", "false");
        p.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        sender.setJavaMailProperties(p);
        return sender;
    }

    /**
     * 发送邮件
     *
     * @param to      接受人[]
     * @param cc      抄送人[]
     * @param subject 主题
     * @param html    发送内容
     * @param isHtml    发送内容是否为html
     * @throws MessagingException           异常
     * @throws UnsupportedEncodingException 异常
     */
    public void sendMail(String[] to,String[] cc, String subject, String html,Boolean isHtml) throws MessagingException, UnsupportedEncodingException {
        MimeMessage mimeMessage = createMailSender().createMimeMessage();
        // 设置utf-8或GBK编码，否则邮件会有乱码
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        messageHelper.setFrom(mailConfigProperties.getMailFrom(), mailConfigProperties.getPersonal());
        messageHelper.setTo(to);
        if (cc!=null&&cc.length>0) {
            messageHelper.setCc(cc);
        }
        messageHelper.setSubject(subject);
        messageHelper.setText(html, isHtml);
        createMailSender().send(mimeMessage);
    }

    /**
     * 发送带附件的邮件
     *
     * @param to      接受人[]
     * @param cc      抄送人[]
     * @param subject 主题
     * @param html    发送内容
     * @param isHtml    发送内容是否为html
     * @param filePaths  附件路径数组
     * @throws MessagingException           异常
     * @throws UnsupportedEncodingException 异常
     */
    public void sendAttachmentMail(String[] to,String[] cc, String subject, String html,Boolean isHtml, String[] filePaths) throws MessagingException, UnsupportedEncodingException {
        MimeMessage mimeMessage = createMailSender().createMimeMessage();
        // 设置utf-8或GBK编码，否则邮件会有乱码
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        messageHelper.setFrom(mailConfigProperties.getMailFrom(), mailConfigProperties.getPersonal());
        messageHelper.setTo(to);
        if (cc!=null&&cc.length>0) {
            messageHelper.setCc(cc);
        }
        messageHelper.setSubject(subject);
        messageHelper.setText(html, isHtml);

        if (filePaths!=null) {
            for (String filePath : filePaths) {
                FileSystemResource file=new FileSystemResource(new File(filePath));
                String fileName=filePath.substring(filePath.lastIndexOf(File.separator));
                messageHelper.addAttachment(fileName,file);
            }
        }
        createMailSender().send(mimeMessage);
    }
}
